using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using StudentManagement.Models;
using Microsoft.Extensions.Configuration;
using Microsoft.EntityFrameworkCore;
using StudentManagement.Infrastructure.Repositories;
using StudentManagement.Services;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Http;
using StudentManagement.Repositories;

namespace StudentManagement
{
    public class Startup
    {
        public IConfiguration Configuration { get; }

        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        // This method gets called by the runtime. Use this method to add services to the container.
        // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
        public void ConfigureServices(IServiceCollection services)
        {
            // database connection
            var connection = Configuration.GetConnectionString("studentDb");
            services.AddDbContext<ApplicationDbContext>(
                options => options.UseSqlServer(connection));

            services.AddControllersWithViews();
            services.AddSession();

            #region authentication
            services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
                    .AddCookie(CookieAuthenticationDefaults.AuthenticationScheme, o =>
                    {
                        //登录路径：这是当用户试图访问资源但未经过身份验证时，程序将会将请求重定向到这个相对路径                        
                        o.LoginPath = new PathString("/Account/Login");
                        //禁止访问路径：当用户试图访问资源时，但未通过该资源的任何授权策略，请求将被重定向到这个相对路径。
                        o.AccessDeniedPath = new PathString("/Home/Privacy");
                    });
            #endregion

            //services.AddSingleton<IStudentRepository, MockStudentRepository>();
            //services.AddTransient<IStudentRepository, MsSqlStudentRepository>();
            services.AddScoped(typeof(IRepository<>), typeof(RepositoryBase<>));
            services.AddScoped<IStudentService, StudentService>();
            services.AddScoped<IUserService, UserService>(); 
            services.AddScoped<ICourseService, CourseService>(); 
            services.AddScoped<ITeacherService, TeacherService>();
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment()) {
                app.UseDeveloperExceptionPage();
            }
            else if (env.IsStaging() || env.IsProduction() || env.IsEnvironment("UAT")) {
                app.UseExceptionHandler("/Error");
                //app.UseHsts();
            }

            //使用种子数据初始化
            app.UseDataInitializer();

            app.UseStatusCodePagesWithReExecute("/Error/ErrorHandler/{0}");

            app.UseStaticFiles();
            app.UseSession();

            app.UseRouting();

            #region authentication
            app.UseAuthentication();
            app.UseAuthorization();
            #endregion

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllerRoute(
                    name: "default",
                    pattern: "{controller=Account}/{action=Login}/{id?}");
            });
        }
    }
}
